package org.example.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.domain.ActivityRecord;
import org.example.domain.User;

@Mapper
public interface ActivityRecordMapper {

    @Select("SELECT * FROM activityRecord WHERE userId=#{userId};")
    ActivityRecord[] getActivityRecordsByUserId(Integer userId);

    @Select("SELECT * FROM (activityRecord,activity) WHERE activityRecord.id IN (#{activityRecordIdOne}," +
            "#{activityRecordIdTwo},#{activityRecordIdThree},#{activityRecordIdFour},#{activityRecordIdFive}," +
            "#{activityRecordIdSix}) AND activity.id=activityRecord.activityId;")
    ActivityRecord[] getActivityRecordsInIds(@Param("activityRecordIdOne") Integer activityRecordIdOne,
                                             @Param("activityRecordIdTwo") Integer activityRecordIdTwo,
                                             @Param("activityRecordIdThree") Integer activityRecordIdThree,
                                             @Param("activityRecordIdFour") Integer activityRecordIdFour,
                                             @Param("activityRecordIdFive") Integer activityRecordIdFive,
                                             @Param("activityRecordIdSix") Integer activityRecordIdSix);

    @Select("SELECT * FROM activityRecord WHERE userId=#{userId} AND activityId=#{activityId};")
    ActivityRecord getActivityRecordByUserIdAndActivityId(@Param("userId") Integer userId
            ,@Param("activityId") Integer activityId);

    @Update("UPDATE activityRecord SET status=0 WHERE id=#{id};")
    void changeStatusToZero(Integer id);//将该条记录设置为未申请/申请失败

    @Update("UPDATE activityRecord SET status=1 WHERE id=#{id};")
    void changeStatusToOne(Integer id);

    @Update("UPDATE activityRecord SET status=2 WHERE id=#{id};")
    void changeStatusToTwo(Integer id);//将该条记录设置为已经申请成功素拓

    @Select("SELECT * FROM activityRecord WHERE id=#{id};")
    ActivityRecord getActivityRecordById(Integer id);

    @Select("SELECT userId FROM activityRecord WHERE activityId=#{activityId};")
    Integer[] getUserIdsByActivityId(Integer activityId);//获取某项活动的所有报名参加人员

    @Insert("INSERT INTO activityRecord (userId,activityId) VALUES (#{userId},#{activityId});")
    void addActivityRecord(@Param("userId") Integer userId,@Param("activityId") Integer activityId);

    @Select("SELECT * FROM activityRecord WHERE activityId=#{activityId}")
    @Results({
            @Result(
                    column = "userId",
                    property = "userId"
            ),
            @Result(
                    column = "userId",
                    property = "user",
                    javaType= User.class,
                    one = @One(select="org.example.Mapper.UserMapper.getAllById")
            )
    })
    ActivityRecord[] getActivityRecordsByActivityId(Integer activityId);//通过活动的id获取该项活动的所有参加记录

    @Update("UPDATE activityRecord SET isDo=1 WHERE id=#{id};")
    void changeIsDoToOneById(Integer id);//修改指定id的活动记录为已签到

    @Update("UPDATE activityRecord SET isDo=2 WHERE id=#{id};")
    void changeIsDoToTwoById(Integer id);//修改指定id的活动记录为缺席

    @Delete("DELETE FROM activityRecord WHERE userId=#{userId};")
    void deleteActivityRecordByUserId(Integer userId);//删除该用户的活动记录

    @Delete("DELETE FROM activityRecord WHERE id=#{id};")
    void deleteActivityRecordById(Integer id);//删除指定id的活动记录

    @Select("SELECT * FROM activityRecord WHERE isDo=2 AND isPunish=0 AND userId=#{userId};")
    ActivityRecord[] getAllActivityRecordsByIsDoAndIsPunishAndUserId(Integer userId);//获取指定用户id所有的缺席
    // 并且还未被处罚的记录

    @Update("UPDATE activityRecord SET isPunish=1 WHERE userId=#{userId};")
    void changeIsPunishToOneByUserId(Integer userId);//对已经做出处罚的学生，将其全部未签到的活动记录标记未已经处罚过来

    @Update("UPDATE activityRecord SET isPunish=0 WHERE id=#{id};")
    void changeIsPunishToZeroByActivityRecordId(Integer id);

    @Delete("DELETE FROM activityRecord WHERE userId=#{userId} AND activityId=#{activityId};")
    void deleteActivityRecordByUserIdAndActivityId(@Param("userId") Integer userId,
                                                   @Param("activityId") Integer activityId);
}
